﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabelTest
{
    public partial class Form1 : Form
    {
        string[] _DataSource = new string[] { "A", "B", "C", "D", "2", "3", "4" };
        int _LX = 25;
        int _LY = 65;
        int _LWidth = 60;
        int _LHeight = 72;
        int _LMaxChar = 8;
        int _YSpace = 20;
        int _XSpace = 10;

        int _WellCounts = 0;
        int _TbX = 0;
        int _TbWidth = 100;
        int _TbHeight = 21;
        TextBox[] textBoxes;
        public Form1()
        {
            InitializeComponent();
            InitializeLabel();
        }

        private void InitializeLabel()
        {
            _WellCounts = _DataSource.Length;
            Label[] labels = new Label[_WellCounts];

            for (int i = 0; i < 2; i++)
            {
                string str = _DataSource[i];
                str = str.Length > _LMaxChar ? str.Substring(0, _LMaxChar - 1) + "..." : str;
                var l = new Label() { Text = str, AutoSize = true };
                l.Font = new Font("黑体", 25);
                l.ForeColor = Color.White;
                l.BackColor = Color.Red;
                if (i == 0)
                {
                    l.Location = new Point(_LX, _LY);
                }
                else
                {
                    _LY = _LY + _YSpace + labels[i - 1].Size.Height;
                    l.Location = new Point(_LX, _LY);
                }
                this.panel1.Controls.Add(l);
                labels[i] = l;
            }
        }

        private void InitializeForm()
        {
            _WellCounts = _DataSource.Length;
            Label[] labels = new Label[_WellCounts];
            textBoxes = new TextBox[_WellCounts];

            for (int i = 0; i < _WellCounts; i++)
            {
                string str = _DataSource[i];
                str = str.Length > _LMaxChar ? str.Substring(0, _LMaxChar - 1) + "..." : str;
                var l = new Label() { Text = str, AutoSize = false, Size = new Size(_LWidth, _LHeight) };
                if (i == 0)
                {
                    l.Location = new Point(_LX, _LY);
                }
                else
                {
                    _LY = _LY + _YSpace + labels[i - 1].Size.Height;
                    l.Location = new Point(_LX, _LY);
                }
                var t = new TextBox() { Size = new Size(_TbWidth, _TbHeight) };
                this.panel1.Controls.Add(l);
                this.panel1.Controls.Add(t);
                labels[i] = l;
                textBoxes[i] = t;
            }

            _TbX = _LX + _LWidth + _XSpace;
            for (int i = 0; i < _WellCounts; i++)
            {
                textBoxes[i].Location = new Point(_TbX, labels[i].Location.Y - 4);
            }
        }

    }
}
